#-*- coding:utf-8 -*-

from pynput.keyboard import Key, Listener
import pynput

import time



# with pynput.keyboard.Events() as event:
#     key_event = event.get(0)
#     print(key_event.key)

time_list = []


def getMS(pre_time):

    return time.time()-pre_time


def saveMS(path=None):

    path = path if path else 'key.ms'
    with open(path,'w',encoding='utf8') as w:
        w.write(str(time_list))


def on_press(key):
    # 监听按键
    print('{0} pressed'.format(key))
    if key == Key.esc:
        saveMS()
        return False
    current_time = time.time()
    print(current_time)
    time_list.append(current_time)
    # print(type(key))
    # print(help(key))
    txt = str(time.time())+'.txt'
    with open(txt,'w') as w:
        w.write('a')

def on_release(key):
    # 监听释放
    print('111111111111111111111111111111111111111')
    print('{0} release'.format(key))
    if key == Key.esc:
        # Stop listener
        return False

# 连接事件以及释放
time_list.append(time.time())
# with Listener(on_press=on_press, on_release=on_release) as listener:
#     listener.join()

listener = Listener(
    on_press=on_press,
    on_release=on_release)
result = listener.start()
# print(result)